About 1271 letters

About 6 minutes

#Python's thread pool

Creating and terminating threads incurs overhead. If threads are repeatedly created and destroyed, program performance may suffer.

In such scenarios, thread reuse is often adopted—threads are not destroyed after finishing execution but are instead paused. When new tasks arise, previously paused threads are resumed to run the new code.

The object that manages these threads is called a thread pool. In Python, you can create a thread pool using the ThreadPoolExecutor class from the concurrent.futures module. The constructor parameter specifies the number of threads.

Example:

from concurrent.futures import ThreadPoolExecutor import time def task(name): print(f"Task {name} started") time.sleep(2) return f"Task {name} completed" # Create a thread pool with 4 threads with ThreadPoolExecutor(max_workers=4) as executor: # Submit tasks to the thread pool futures = [executor.submit(task, i) for i in range(10)] for future in futures: print(future.result()) # Get the results in order of completion

Created in 5/15/2025

Updated in 5/21/2025